class_name EventDispatcher

var _handlers = {}

func _init() -> void:
	pass

func AddEventListener(event_name, func_name, obj):
	if not _handlers.get(event_name):
		_handlers[event_name] = []
	var callback = Callable(func_name, obj)
	if callback in _handlers[event_name]:
		print("error event_name = ", event_name, ", callback = ", callback)
		return
	_handlers[event_name].append(callback)

func DelEventListener(event_name, func_name, obj):
	if not _handlers.get(event_name):
		return
	var callback = Callable(func_name, obj)
	if callback not in _handlers[event_name]:
		return
	_handlers[event_name].erase(callback)


func DispatchEvent(event_name, args_ctx):
	if not _handlers.get(event_name):
		return
	for callback in _handlers[event_name]:
		callback.call(event_name, args_ctx)

func Clear():
	_handlers = {}